package org.jrad.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.jrad.model.Attribute;
import org.jrad.model.EntityCrud;
import org.jrad.model.PersistenceUnitData;

public class JRadHelper {

	private static String attributeTemplate = "\t@Column(length=<size>)\n\tprivate <type> <name>;\n";
	private static String idTemplate = "\t@Id\n\tprivate long id;\n";
	private static String getMethodTemplate = "\tpublic <type> get<name>(){\n\t\treturn this.<name>;\n\t}\n";
	private static String setMethodTemplate = "\tpublic void set<name>(<type> <name>){\n\t\tthis.<name> = <name>;\n\t}\n";
	private static String classTemplate = "public class <name> {\n<content>}";
	
	public static String createAttributes(List<Attribute> attributes) {
		StringBuilder builder = new StringBuilder();
		for (Attribute attribute : attributes) {
			builder.append(attributeTemplate.replaceFirst("<size>", attribute.getSize())
					.replaceFirst("<type>", attribute.getType())
				    .replaceFirst("<name>", attribute.getName()));
		}
		return builder.toString();
	}

	public static String createAttributeId() {
		return idTemplate;
	}

	public static String createGetMethod(Attribute attribute) {
		return getMethodTemplate.replaceAll("<type>", attribute.getType())
				.replaceFirst("<name>", upFirst(attribute.getName()))
				.replaceFirst("<name>", attribute.getName());
	}

	public static String createSetMethod(Attribute attribute) {
		return setMethodTemplate.replaceAll("<type>", attribute.getType())
				.replaceFirst("<name>", upFirst(attribute.getName()))
				.replaceAll("<name>", attribute.getName());
	}

	public static String createClass(EntityCrud entityCrud) {
		return classTemplate.replaceFirst("<name>", entityCrud.getName());
	}
	
	public static String createEntity(EntityCrud entityCrud, String packageRoot) {
		//classContent = "package "+project.getPackageRoot()+";\n"+classContent;
		return "package "+packageRoot+".entity;\n"+getEntityImports()+"@Entity\n"+createClass(entityCrud);
	}	
	
	public static String createPersistenceUnitData(PersistenceUnitData pud){
		String puPath = "template"+File.separator+"persistence.xml";
		String fileInputData = "";
		try {
			fileInputData = readFile(puPath);
			fileInputData = fileInputData.replaceFirst("<persistence_unit_name>" , pud.getName());
			fileInputData = fileInputData.replaceFirst("<url>" , pud.getUrl());
			fileInputData = fileInputData.replaceFirst("<user>" , pud.getUser());
			fileInputData = fileInputData.replaceFirst("<password>" , pud.getPassword());
			fileInputData = fileInputData.replaceFirst("<driver>" , pud.getDriver());
			fileInputData = fileInputData.replaceFirst("<provider_name>" , pud.getProvider());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileInputData;
	}
	
	public static String createDAO(String packageRoot){
		String puPath = "template"+File.separator+"GenericDAO.java";
		String fileInputData = "";
		try {
			fileInputData = readFile(puPath);
			fileInputData = fileInputData.replaceFirst("<packageRoot>" , packageRoot);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileInputData;
	}
	
	public static String createConnection(String packageRoot, String persistenceUnitName){
		String puPath = "template"+File.separator+"Connection.java";
		String fileInputData = "";
		try {
			fileInputData = readFile(puPath);
			fileInputData = fileInputData.replaceFirst("<packageRoot>" , packageRoot);			
			fileInputData = fileInputData.replaceFirst("<persistenceUnitName>" , persistenceUnitName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileInputData;
	}
	
	private static String getEntityImports(){
		return "import javax.persistence.Id;\n" +
			   "import javax.persistence.Entity;\n"+
			   "import javax.persistence.Column;\n";
	}
	
	private static String upFirst(String string) {
		return string.substring(0, 1).toUpperCase()
				+ string.substring(1, string.length());
	}
	
	public static String readFile(String puPath) throws FileNotFoundException, IOException {
		String fileInputData = "";
		BufferedReader buf = new BufferedReader(new FileReader(puPath));			
		while(buf.ready()){
			fileInputData += buf.readLine()+"\n";
		}
		return fileInputData;
	}
	
	public static void writeFile(String filePath, String data) throws FileNotFoundException, IOException {		
		FileOutputStream outputStream = new FileOutputStream(filePath);
		outputStream.write(data.getBytes());
		outputStream.close();
	}

}
